/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadastroServidorOPC.java
 *
 * Created on 16/12/2010, 16:56:18
 */

package br.ufrn.engcomp.pds.helloopc.visao;

import br.ufrn.engcomp.pds.helloopc.controle.CadastrarServidorOPCController;
import br.ufrn.engcomp.pds.helloopc.controle.ICadastroController;
import br.ufrn.engcomp.pds.helloopc.modelo.dominio.ServidorOPC;
import br.ufrn.engcomp.pds.helloopc.modelo.dominio.TagOPC;
import br.ufrn.engcomp.pds.helloopc.modelo.dominio.TipoVariavelOPC;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Felipe Rivas
 */
public class CadastroServidorOPC extends CadastroBase<ServidorOPC> {

    private static final int COLUNA_ATUALIZAR_TABELA = 4;
    private static final int COLUNA_REMOVER_TABELA = 5;
    private static final int COLUNA_ATUALIZAR_TABELA_TAG_OPC = 2;
    private static final int COLUNA_REMOVER_TABELA_TAG_OPC = 3;

    private CadastrarServidorOPCController controller = new CadastrarServidorOPCController();
    private Operacao operacaoAtualTagOPC;
    private TagOPC entidadeTagOPC;
    private List<TagOPC> tagsOPC;

    /** Creates new form CadastroServidorOPC */
    public CadastroServidorOPC() {
        initComponents();

        preencherTabela();

        adicionarBotoesTabela();

        listarTiposVariavelOPC();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        janelaEdicao = new javax.swing.JDialog();
        jPanel2 = new javax.swing.JPanel();
        txtServerName = new javax.swing.JTextField();
        txtNome = new javax.swing.JTextField();
        txtHost = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jnomeOPCLabel = new javax.swing.JLabel();
        txtNomeTagOPC = new javax.swing.JTextField();
        jTipoOPCLabel = new javax.swing.JLabel();
        cmbTipoTagOPC = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabelaTagOPC = new javax.swing.JTable();
        btnOKTagOPC = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        btnCancelar = new javax.swing.JButton();
        btnOK = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabela = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        btnInserir = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(helloopc.HelloOPCApp.class).getContext().getResourceMap(CadastroServidorOPC.class);
        janelaEdicao.setTitle(resourceMap.getString("janelaEdicao.title")); // NOI18N
        janelaEdicao.setModalityType(java.awt.Dialog.ModalityType.TOOLKIT_MODAL);
        janelaEdicao.setName("janelaEdicao"); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N

        txtServerName.setName("txtServerName"); // NOI18N

        txtNome.setName("txtNome"); // NOI18N

        txtHost.setName("txtHost"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jSeparator1.setName("jSeparator1"); // NOI18N

        jnomeOPCLabel.setText(resourceMap.getString("jnomeOPCLabel.text")); // NOI18N
        jnomeOPCLabel.setName("jnomeOPCLabel"); // NOI18N

        txtNomeTagOPC.setText(resourceMap.getString("txtNomeTagOPC.text")); // NOI18N
        txtNomeTagOPC.setName("txtNomeTagOPC"); // NOI18N

        jTipoOPCLabel.setText(resourceMap.getString("jTipoOPCLabel.text")); // NOI18N
        jTipoOPCLabel.setName("jTipoOPCLabel"); // NOI18N

        cmbTipoTagOPC.setName("cmbTipoTagOPC"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        tabelaTagOPC.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome", "Tipo", "Atualizar", "Remover"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Object.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabelaTagOPC.setName("tabelaTagOPC"); // NOI18N
        tabelaTagOPC.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tabelaTagOPC.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaTagOPCMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tabelaTagOPC);
        tabelaTagOPC.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("tabelaTagOPC.columnModel.title0")); // NOI18N
        tabelaTagOPC.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("tabelaTagOPC.columnModel.title1")); // NOI18N
        tabelaTagOPC.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("tabelaTagOPC.columnModel.title2")); // NOI18N
        tabelaTagOPC.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("tabelaTagOPC.columnModel.title3")); // NOI18N

        btnOKTagOPC.setText(resourceMap.getString("btnOKTagOPC.text")); // NOI18N
        btnOKTagOPC.setName("btnOKTagOPC"); // NOI18N
        btnOKTagOPC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKTagOPCActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel4))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel3))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txtNome, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtHost, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtServerName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel1)))
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGap(7, 7, 7)
                            .addComponent(jTipoOPCLabel)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(cmbTipoTagOPC, 0, 0, Short.MAX_VALUE))
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtNomeTagOPC, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jnomeOPCLabel))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnOKTagOPC, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtHost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtServerName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jnomeOPCLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtNomeTagOPC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(28, 28, 28)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jTipoOPCLabel)
                                    .addComponent(cmbTipoTagOPC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                                .addComponent(btnOKTagOPC))
                            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        jPanel3.setName("jPanel3"); // NOI18N

        btnCancelar.setText(resourceMap.getString("btnCancelar.text")); // NOI18N
        btnCancelar.setName("btnCancelar"); // NOI18N
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnOK.setText(resourceMap.getString("btnOK.text")); // NOI18N
        btnOK.setName("btnOK"); // NOI18N
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(275, Short.MAX_VALUE)
                .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnCancelar)
                .addGap(305, 305, 305))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnOK)
                    .addComponent(btnCancelar))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout janelaEdicaoLayout = new javax.swing.GroupLayout(janelaEdicao.getContentPane());
        janelaEdicao.getContentPane().setLayout(janelaEdicaoLayout);
        janelaEdicaoLayout.setHorizontalGroup(
            janelaEdicaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        janelaEdicaoLayout.setVerticalGroup(
            janelaEdicaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(janelaEdicaoLayout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Nome", "Host", "Server Name", "Atualizar", "Remover"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabela.setName("tabela"); // NOI18N
        tabela.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tabela.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabela);
        tabela.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("tabela.columnModel.title0")); // NOI18N
        tabela.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("tabela.columnModel.title1")); // NOI18N
        tabela.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("tabela.columnModel.title2")); // NOI18N
        tabela.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("tabela.columnModel.title3")); // NOI18N
        tabela.getColumnModel().getColumn(4).setHeaderValue(resourceMap.getString("tabela.columnModel.title4")); // NOI18N
        tabela.getColumnModel().getColumn(5).setHeaderValue(resourceMap.getString("tabela.columnModel.title5")); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        btnInserir.setText(resourceMap.getString("btnInserir.text")); // NOI18N
        btnInserir.setName("btnInserir"); // NOI18N
        btnInserir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInserirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(152, 152, 152)
                .addComponent(btnInserir)
                .addContainerGap(157, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnInserir)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnInserirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInserirActionPerformed
        // TODO add your handling code here:
        processarAcaoBotaoInserir();
}//GEN-LAST:event_btnInserirActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        processarAcaoBotaoCancelar();
}//GEN-LAST:event_btnCancelarActionPerformed

    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
        // TODO add your handling code here:
        processarAcaoBotaoOK();
}//GEN-LAST:event_btnOKActionPerformed

    private void btnOKTagOPCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKTagOPCActionPerformed
        // TODO add your handling code here:
        processarAcaoBotaoOKTagOPC();
    }//GEN-LAST:event_btnOKTagOPCActionPerformed

    private void tabelaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaMouseClicked
        // TODO add your handling code here:
        if (getTabela().getSelectedColumn() == COLUNA_ATUALIZAR_TABELA) {
            atualizar();
        } else if (getTabela().getSelectedColumn() == COLUNA_REMOVER_TABELA) {
            remover();
        }
    }//GEN-LAST:event_tabelaMouseClicked

    private void tabelaTagOPCMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaTagOPCMouseClicked
        // TODO add your handling code here:
        if (tabelaTagOPC.getSelectedColumn() == COLUNA_ATUALIZAR_TABELA_TAG_OPC) {
            atualizarTagsOPC();
        } else if (tabelaTagOPC.getSelectedColumn() == COLUNA_REMOVER_TABELA_TAG_OPC) {
            removerTagsOPC();
        }
    }//GEN-LAST:event_tabelaTagOPCMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnInserir;
    private javax.swing.JButton btnOK;
    private javax.swing.JButton btnOKTagOPC;
    private javax.swing.JComboBox cmbTipoTagOPC;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel jTipoOPCLabel;
    private javax.swing.JDialog janelaEdicao;
    private javax.swing.JLabel jnomeOPCLabel;
    private javax.swing.JTable tabela;
    private javax.swing.JTable tabelaTagOPC;
    private javax.swing.JTextField txtHost;
    private javax.swing.JTextField txtNome;
    private javax.swing.JTextField txtNomeTagOPC;
    private javax.swing.JTextField txtServerName;
    // End of variables declaration//GEN-END:variables

    @Override
    protected ICadastroController getController() {
        return controller;
    }

    @Override
    protected JDialog getJanelaEdicao() {
        return this.janelaEdicao;
    }

    @Override
    protected JTable getTabela() {
        return this.tabela;
    }

    @Override
    protected void setTextoBotaoOK(String texto) {
        this.btnOK.setText(texto);
    }

    @Override
    protected void limparJanelaEdicao() {
        txtNome.setText("");
        txtHost.setText("");
        txtServerName.setText("");

        tagsOPC = new ArrayList<TagOPC>();

        if (operacaoAtual == Operacao.ATUALIZAR) {
            tagsOPC.addAll(controller.sincronizarTagsOPC(entidade));
        }

        for (int i=tabelaTagOPC.getRowCount() - 1; i >= 0; i--) {
            ((DefaultTableModel)tabelaTagOPC.getModel()).removeRow(i);

            ((DefaultTableModel)tabelaTagOPC.getModel()).fireTableRowsDeleted(i, i);
        }

        limparJanelaEdicaoTagOPC();
    }

    @Override
    protected void preencherJanelaEdicao() {
        txtNome.setText(entidade.getNome());
        txtHost.setText(entidade.getHost());
        txtServerName.setText(entidade.getServerName());

        preencherTabelaTagsOPC();
    }

    @Override
    protected void preencherTabela() {
        try {
            List<ServidorOPC> servidoresOPC = controller.listar();

            for (ServidorOPC s : servidoresOPC) {
                atualizarTabela(s, Operacao.INSERIR);
            }
        } catch (Exception ex) {
            //mostrarErro("Não foi possível inserir/atualizar registro da tabela.");
        }
    }

    protected void preencherTabelaTagsOPC(){
        try {
            for (TagOPC tagOPC : tagsOPC) {
                ((DefaultTableModel)tabelaTagOPC.getModel()).addRow(this.getLinhaDadosTagOPC(tagOPC));

                ((DefaultTableModel)tabelaTagOPC.getModel()).fireTableRowsInserted(tabelaTagOPC.getRowCount(), tabelaTagOPC.getRowCount());
            }
        } catch (Exception ex) {
            //mostrarErro("Não foi possível inserir/atualizar registro da tabela.");
        }
    }

    @Override
    protected void preencherEntidade() {
        entidade.setNome(txtNome.getText());
        entidade.setHost(txtHost.getText());
        entidade.setServerName(txtServerName.getText());

        for (TagOPC tagOPC : tagsOPC) {
            tagOPC.setServidorOPC(entidade);
        }

        entidade.setTagsOPC(tagsOPC);
    }

    @Override
    protected String getDescricaoEntidade() {
        return "Servidor OPC";
    }

    @Override
    protected Object[] getLinhaDados(ServidorOPC entidade) {
        return new Object[] {
            entidade.getId(),
            entidade.getNome(),
            entidade.getHost(),
            entidade.getServerName(),
            "A",
            "R"
        };
    }

    protected Object[] getLinhaDadosTagOPC(TagOPC tagOPC){
        return new Object[] {
            tagOPC.getNome(),
            tagOPC.getTipo(),
            "A",
            "R"
        };
    }

    private void processarAcaoBotaoOKTagOPC() {
        try {
            if (operacaoAtualTagOPC == Operacao.INSERIR) {
                entidadeTagOPC = new TagOPC();

                preencherEntidadeTagOPC();

                tagsOPC.add(entidadeTagOPC);
            } else if (operacaoAtualTagOPC == Operacao.ATUALIZAR) {
                preencherEntidadeTagOPC();

                tagsOPC.set(tagsOPC.indexOf(entidadeTagOPC), entidadeTagOPC);
            }

            atualizarTabelaTagOPC(entidadeTagOPC);

            limparJanelaEdicaoTagOPC();
        } catch (Exception ex) {
            ex.printStackTrace();
            //mostrarErro("Não foi possível inserir/atualizar registro da tabela.");
        }
    }

    private void preencherEntidadeTagOPC() {
        entidadeTagOPC.setNome(txtNomeTagOPC.getText());
        entidadeTagOPC.setTipo(TipoVariavelOPC.valueOf(cmbTipoTagOPC.getSelectedItem().toString()));
    }

    private void atualizarTagsOPC() {
        limparJanelaEdicaoTagOPC();

        entidadeTagOPC = tagsOPC.get(tabelaTagOPC.getSelectedRow());

        operacaoAtualTagOPC = Operacao.ATUALIZAR;

        if (operacaoAtualTagOPC == Operacao.INSERIR) {
            btnOKTagOPC.setText("Inserir");
        } else if (operacaoAtualTagOPC == Operacao.ATUALIZAR) {
            btnOKTagOPC.setText("Atualizar");

            preencherJanelaEdicaoTagOPC();
        }
    }

    private void removerTagsOPC() {
        try {
            operacaoAtualTagOPC = Operacao.REMOVER;

            if (Confirmar(janelaEdicao, "Tem certeza que deseja excluir este registro?") == JOptionPane.OK_OPTION) {
                tagsOPC.remove(tabelaTagOPC.getSelectedRow());

                atualizarTabelaTagOPC(null);

                limparJanelaEdicaoTagOPC();

                if (tabelaTagOPC.getRowCount() > 0) {
                    tabelaTagOPC.getSelectionModel().setSelectionInterval(0, 0);
                }
            }
        } catch (Exception ex) {
            //mostrarErro("Não foi possível remover registro da tabela.");
        }
    }

    private void atualizarTabelaTagOPC(TagOPC entidadeTagOPC) throws Exception {
        switch (operacaoAtualTagOPC) {
            case INSERIR: {
                    ((DefaultTableModel)tabelaTagOPC.getModel()).addRow(this.getLinhaDadosTagOPC(entidadeTagOPC));

                    ((DefaultTableModel)tabelaTagOPC.getModel()).fireTableRowsInserted(tabelaTagOPC.getRowCount(), tabelaTagOPC.getRowCount());
                }
                break;
            case ATUALIZAR: {
                    int linhaSelecionada = tabelaTagOPC.getSelectedRow();
                    
                    ((DefaultTableModel)tabelaTagOPC.getModel()).removeRow(linhaSelecionada);
                    ((DefaultTableModel)tabelaTagOPC.getModel()).insertRow(linhaSelecionada, this.getLinhaDadosTagOPC(entidadeTagOPC));

                    ((DefaultTableModel)tabelaTagOPC.getModel()).fireTableRowsUpdated(linhaSelecionada, linhaSelecionada);
                }
                break;
            case REMOVER: {
                    int linhaSelecionada = tabelaTagOPC.getSelectedRow();

                    ((DefaultTableModel)tabelaTagOPC.getModel()).removeRow(linhaSelecionada);

                    ((DefaultTableModel)tabelaTagOPC.getModel()).fireTableRowsDeleted(linhaSelecionada, linhaSelecionada);
                }
                break;
            default:
                throw new Exception("Tipo de operação inválida.");
        }
    }

    private void limparJanelaEdicaoTagOPC() {
        operacaoAtualTagOPC = Operacao.INSERIR;

        btnOKTagOPC.setText("Inserir");

        entidadeTagOPC = null;
        
        txtNomeTagOPC.setText("");
        cmbTipoTagOPC.setSelectedIndex(0);
    }

    private void preencherJanelaEdicaoTagOPC() {
        txtNomeTagOPC.setText(entidadeTagOPC.getNome());
        cmbTipoTagOPC.setSelectedItem(entidadeTagOPC.getTipo());
    }

    private void listarTiposVariavelOPC() {
        ((DefaultComboBoxModel) cmbTipoTagOPC.getModel()).removeAllElements();

        for (TipoVariavelOPC tipo : TipoVariavelOPC.values()) {
            cmbTipoTagOPC.addItem(tipo);
        }
    }

    @Override
    protected void adicionarBotoesTabela() {
        ButtonColumn bcAtualizar = new ButtonColumn(getTabela(), COLUNA_ATUALIZAR_TABELA);
        ButtonColumn bcRemover = new ButtonColumn(getTabela(), COLUNA_REMOVER_TABELA);

        ButtonColumn bcAtualizarTagOPC = new ButtonColumn(tabelaTagOPC, COLUNA_ATUALIZAR_TABELA_TAG_OPC);
        ButtonColumn bcRemoverTagOPC = new ButtonColumn(tabelaTagOPC, COLUNA_REMOVER_TABELA_TAG_OPC);
    }

}
